import { type CarbonReductionParams, getCarbonReductionYear } from '../apis';
import { type FilterMenuProps, ComponentType } from 'vc/src/web';

export function useScreen() {
  const queryData = reactive<CarbonReductionParams>({
    pageNum: 1,
    pageSize: 15,
    indicatorFiled: 'numberOfSupportedProjects',
    indicatorOrder: 'desc',
    carbonReductionTerritoryCode: '4',
    statisticalPeriod: '',
    reportingPeriodTypeCode: '2',
    valueTypeCode: '1'
  });

  const tabs = [
    { name: '合计', code: '4' },
    { name: '清洁能源', code: '1' },
    { name: '节能环保', code: '2' },
    { name: '碳减排技术', code: '3' }
  ];
  const changeTab = (code: string) => {
    queryData.carbonReductionTerritoryCode = code;
  };

  const menus = ref<Array<FilterMenuProps>>([
    {
      ...getYear(queryData),
      required: true,
      submitFn: (val: string) => {
        queryData.statisticalPeriod = val;
      }
    },
    {
      title: '报告期',
      component: ComponentType.SINGLE,
      required: true,
      options: [
        { name: '一季度', code: '1' },
        { name: '二季度', code: '2' },
        { name: '三季度', code: '3' },
        { name: '四季度', code: '4' },
        { name: '半年度', code: '5' },
        { name: '年度', code: '6' }
      ],
      value: '2',
      submitFn: (val: string) => {
        if (val === '5') {
          menus.value[2].options = [
            { name: '半年度数据', code: '1' },
            { name: '本年数据', code: '2' },
            { name: '历史累计', code: '3' }
          ];
          menus.value[2].value = '1';
          queryData.valueTypeCode = '1';
        } else if (val === '6') {
          menus.value[2].options = [
            { name: '本年数据', code: '2' },
            { name: '历史累计', code: '3' }
          ];
          menus.value[2].value = '2';
          queryData.valueTypeCode = '2';
        } else {
          menus.value[2].options = [
            { name: '当季数据', code: '1' },
            { name: '本年数据', code: '2' },
            { name: '历史累计', code: '3' }
          ];
          menus.value[2].value = '1';
          queryData.valueTypeCode = '1';
        }
        queryData.reportingPeriodTypeCode = val;
      }
    },
    {
      title: '值类型',
      component: ComponentType.SINGLE,
      required: true,
      options: [
        { name: '当季数据', code: '1' },
        { name: '本年数据', code: '2' },
        { name: '历史累计', code: '3' }
      ],
      value: '1',
      submitFn: (val: string) => {
        queryData.valueTypeCode = val;
      }
    }
  ]);

  const traceSource = ref(false);

  return {
    queryData,
    tabs,
    changeTab,
    menus,
    traceSource
  };
}

function getYear(queryData: CarbonReductionParams) {
  const { data } = useRequest(getCarbonReductionYear);
  const options = computed(() => {
    if (data.value) {
      queryData.statisticalPeriod = data.value[0].statisticalYear + '';
      return data.value.map((d) => {
        return { name: d.statisticalYear + '年', code: d.statisticalYear + '' };
      });
    } else {
      return [];
    }
  });
  const value = computed(() => {
    return options.value.length ? queryData.statisticalPeriod : '';
  });

  return {
    title: '数据年度',
    component: ComponentType.SINGLE,
    options,
    value,
    submitFn: () => {}
  };
}
